package org.eenie.core.base.viewmodel

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.viewbinding.ViewBinding
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import org.eenie.core.base.viewbinding.BaseBindingDialogFragment
import org.eenie.core.base.viewbinding.BaseBindingFragment

abstract class BaseBindingViewModelDialogFragment<BD : ViewBinding, VS : Any, VM : BaseViewModel<VS>> : BaseBindingDialogFragment<BD>() {
    lateinit var viewModel: VM

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        viewModel = providerViewModel()
        super.onViewCreated(view, savedInstanceState)
        viewModel.state.onEach {
            renderUI(it)
        }.launchIn(lifecycleScope)
    }

    abstract fun providerViewModel(): VM
    abstract fun renderUI(state: VS)

}